package com.fk.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fk.dao.impl.ChanPinFenLeiDaoImpl;
import com.fk.dao.impl.ShangPinFenLeiDaoImpl;
import com.fk.entity.ShangPinFenLei;
import com.fk.uitl.BaseServlet;
import com.fk.uitl.Result;
import com.google.gson.Gson;
import com.sun.crypto.provider.RSACipher;


public class AdminProductDetailServlet extends BaseServlet {
	
	/**
	 * 获得商品详情
	 * localhost:8080/wuyijiuchaoliufushiguan/adminProductDetail?reqName=getShangPinXiangQing
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getShangPinXiangQing(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		ShangPinFenLeiDaoImpl shang = new ShangPinFenLeiDaoImpl(); // 创建实现类
		List<ShangPinFenLei> shangList = shang.getShangPinFenLeiList();//调用方法
		
		PrintWriter out = response.getWriter();	// out对象
		response.setCharacterEncoding("UTF-8");	// 设置编码格式
		
		Gson gson = new Gson();
		String res = gson.toJson(shangList);
		out.print(res);
		
		
		out.flush();
		out.close();
	}
	/**
	 * 根据商品详情id删除商品
	 * localhost:8080/wuyijiuchaoliufushiguan/adminProductDetail?reqName=delShangPinXiangQing
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void delShangPinXiangQing(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			
			int sId = Integer.parseInt(request.getParameter("sId"));
		
			ShangPinFenLeiDaoImpl shang = new ShangPinFenLeiDaoImpl(); // 创建实现类
			int s = shang.deleteShangPinFenLei(sId);		//调用方法
			
			response.setCharacterEncoding("UTF-8");	// 设置编码格式
			PrintWriter out = response.getWriter();	// out对象
			
			
			if (s > 0) {
				out.print(Result.toClient(true, "删除成功", ""));
			} else {
				out.print(Result.toClient(false, "删除失败", ""));
			}
			
			
			out.flush();
			out.close();
		}
	
	/**
	 * localhost:8080/wuyijiu/adminProductDetail?reqName=getShangPinList
	 * 商品多条件查询
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getShangPinList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
			//接受前台传来的参数
			int fenLeiId=0;
			if(request.getParameter("fenLeiId")!=null && !request.getParameter("fenLeiId").equals("")) {
				fenLeiId=Integer.parseInt(request.getParameter("fenLeiId"));
			}
			
			String name=request.getParameter("name");
			String img=request.getParameter("img");
			String caiZhi=request.getParameter("caiZhi");
			
			int beginShiChangJia=0;//开始价格
			if(request.getParameter("beginShiChangJia")!=null && !request.getParameter("beginShiChangJia").equals("")) {
				beginShiChangJia=Integer.parseInt(request.getParameter("beginShiChangJia"));
			}
			
			int endShiChangJia=0;//结束价格
			if(request.getParameter("endShiChangJia")!=null && !request.getParameter("endShiChangJia").equals("")) {
				endShiChangJia=Integer.parseInt(request.getParameter("endShiChangJia"));
			}
			
			int xianJia=0;//结束价格
			if(request.getParameter("xianJia")!=null && !request.getParameter("xianJia").equals("")) {
				xianJia=Integer.parseInt(request.getParameter("xianJia"));
			}
			
			String faHuoDiZhi=request.getParameter("faHuoDiZhi");
			String kuanShi=request.getParameter("kuanShi");
			String yangShi=request.getParameter("yangShi");
			
			int chanPinId=0;//结束价格
			if(request.getParameter("chanPinId")!=null && !request.getParameter("chanPinId").equals("")) {
				chanPinId=Integer.parseInt(request.getParameter("chanPinId"));
			}
			
			//根据接受的参数创建产品详情对象
			ShangPinFenLei p=new ShangPinFenLei(fenLeiId, name, img, caiZhi, 0, beginShiChangJia, endShiChangJia, xianJia, faHuoDiZhi, kuanShi, yangShi, chanPinId,"" );
			
			ShangPinFenLeiDaoImpl spDao=new ShangPinFenLeiDaoImpl();
			List<ShangPinFenLei> spList=spDao.getproductsByWhere(p);//查询所有产品详情对象集合
			
			
			
			response.setCharacterEncoding("UTF-8");//设置响应的编码格式
			PrintWriter out=response.getWriter();//out对象
			
			Gson gson=new Gson();//Gson对象
			String res=gson.toJson(spList);//将spList转化为json字符串
			
			out.print(res);//响应数据
			
			out.flush();
			out.close();
		}	
	/**
	 * 添加产品
	 * localhost:8080/wuyijiu/adminProductDetail?reqName=addProducts
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void addProducts(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		
		String name =request.getParameter("name");
		String img=request.getParameter("img");
		String caiZhi=request.getParameter("caiZhi");
		int shiChangJia = Integer.parseInt(request.getParameter("shiChangJia"));
		int xianJia = Integer.parseInt(request.getParameter("xianJia"));
		String faHuoDiZhi=request.getParameter("faHuoDiZhi");
		String kuanShi=request.getParameter("kuanShi");
		String yangShi=request.getParameter("yangShi");
		int chanPinId = Integer.parseInt(request.getParameter("chanPinId"));
		
		ShangPinFenLeiDaoImpl sDao = new ShangPinFenLeiDaoImpl();
		
		if(name != null && ! name.equals("")) {
			List<ShangPinFenLei> sList = sDao.getShangPinFenLeiName(name);
			if(sList.size() == 0) {
				ShangPinFenLei shang = new ShangPinFenLei(0, name, img, caiZhi, shiChangJia, xianJia, faHuoDiZhi, kuanShi, yangShi, chanPinId);
				int s = sDao.addProductDetail(shang);
				if(s>0) {
					out.print(Result.toClient(true, "添加成功", ""));
				}else {
					out.print(Result.toClient(false, "添加失败", ""));
				}
			}else {
				out.print(Result.toClient(false, "商品名称已存在", ""));
			}
		}
		
		
		out.flush();
		out.close();
		
	}
	/**
	 * 根据id查询商品
	 * localhost:8080/wuyijiu/adminProductDetail?reqName=updateShangPinId
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void updateShangPinId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		
		int sId = Integer.parseInt(request.getParameter("sId"));
		ShangPinFenLeiDaoImpl sDao = new ShangPinFenLeiDaoImpl();
		ShangPinFenLei shang = sDao.getShangPinFenLeiId(sId);
		
		Gson gson = new Gson();
		
		String res = gson.toJson(shang);
		out.print(res);
		
		out.flush();
		out.close();
	}
	/**
	 * 根据id修改产品
	 * localhost:8080/wuyijiu/adminProductDetail?reqName=updateShangPin
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void updateShangPin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		ShangPinFenLeiDaoImpl sDao = new ShangPinFenLeiDaoImpl();

		int fenLeiId=0;
		if(request.getParameter("fenLeiId")!=null && !request.getParameter("fenLeiId").equals("")) {
			fenLeiId=Integer.parseInt(request.getParameter("fenLeiId"));
		}
		
		String name=request.getParameter("name");
		String img=request.getParameter("img");
		String caiZhi=request.getParameter("caiZhi");
		
		int shiChangJia = 0;
		if(request.getParameter("shiChangJia")!=null && !request.getParameter("shiChangJia").equals("")) {
			shiChangJia=Integer.parseInt(request.getParameter("shiChangJia"));
		}
		
		
		int xianJia=0;//�����۸�
		if(request.getParameter("xianJia")!=null && !request.getParameter("xianJia").equals("")) {
			xianJia=Integer.parseInt(request.getParameter("xianJia"));
		}
		
		String faHuoDiZhi=request.getParameter("faHuoDiZhi");
		String kuanShi=request.getParameter("kuanShi");
		String yangShi=request.getParameter("yangShi");
		
		int chanPinId=0;//�����۸�
		if(request.getParameter("chanPinId")!=null && !request.getParameter("chanPinId").equals("")) {
			chanPinId=Integer.parseInt(request.getParameter("chanPinId"));
		}
		
		ShangPinFenLei shang=new ShangPinFenLei(fenLeiId, name, img, caiZhi, shiChangJia, xianJia, faHuoDiZhi, kuanShi, yangShi, chanPinId);
		System.out.println(shang);
		int s = sDao.updateGetShangPin(shang);
		if(s>0) {
			out.print(Result.toClient(true, "编辑成功", ""));
		}else {
			out.print(Result.toClient(false, "编辑失败", ""));
		}
		out.flush();
		out.close();
	}
}

